package com.demo.server.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;
import org.mybatis.generator.internal.util.StringUtility;

public class MybatisPlugin extends PluginAdapter {

	@Override
	public boolean validate(List<String> warnings) {
		// TODO Auto-generated method stub
		return true;
	}
	
	@Override
    public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
 
        // 添加实体类的 import
        topLevelClass.addImportedType("lombok.Data");
        //topLevelClass.addImportedType("lombok.ToString");
        topLevelClass.addImportedType("io.swagger.v3.oas.annotations.media.Schema");
        // 添加实体类的 lombok 注解
        topLevelClass.addAnnotation("@Data");
        //topLevelClass.addAnnotation("@ToString");
        if(StringUtility.stringHasValue(introspectedTable.getRemarks())) {
        	topLevelClass.addAnnotation("@Schema(description = \""+introspectedTable.getRemarks()+"\")");
        }
        return true;
    }
	
	public boolean modelFieldGenerated(Field field, TopLevelClass topLevelClass,
            IntrospectedColumn introspectedColumn,
            IntrospectedTable introspectedTable, ModelClassType modelClassType) {
		 if(StringUtility.stringHasValue(introspectedColumn.getRemarks())) {
			 field.addAnnotation("@Schema(description = \""+introspectedColumn.getRemarks()+"\")");
		 }
        return true;
    }

	@Override
    public boolean modelSetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        // 不生成 setter
        return false;
    }
 
    @Override
    public boolean modelGetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        // 不生成 getter
        return false;
    }
    
    public static void main(String[] args) throws Exception, IOException {
		 List<String> warnings = new ArrayList<String>();
	        InputStream in = MybatisPlugin.class.getClassLoader().getResourceAsStream("generatorConfig.xml");
	        ConfigurationParser cp = new ConfigurationParser(warnings);
	        Configuration config = cp.parseConfiguration(in);
	        in.close();
	        // 不覆盖 Java 文件
	        boolean overwrite = true;
	        DefaultShellCallback callback = new DefaultShellCallback(overwrite);
	        MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);

	        // 生成文件
	        myBatisGenerator.generate(null);
	        // 打印信息
	        warnings.forEach(System.err::println);
	}

}
